unit uCxtForm;

interface

uses
  SysUtils, Classes, Controls, Graphics, Forms, Dialogs, StdCtrls, ExtCtrls, Messages,
  Windows, ShellAPI, uDZPersistent, uCxtTypes;

type
  TBaseForm = class(TForm)
  protected
    function ShowTaskBarIcon: Boolean; dynamic;
    function CenterForm: Boolean; dynamic;
    procedure WMSize(var msgr: TWMSize); message WM_SIZE;
    procedure CreateParams(var Params: TCreateParams); override;
  end;

  TLBSObjectForm = class(TBaseForm)
  private
    fObjType: TLBSObjectType;
    fObj: TLBSObject;
  private
    procedure SetObj(const Value: TLBSObject);
    procedure SetObjType(const Value: TLBSObjectType);
  protected
    procedure Changed; virtual; abstract;
  public
    destructor Destroy; override;
    property ObjType: TLBSObjectType read fObjType write SetObjType;
    property Obj: TLBSObject read fObj write SetObj;
  end;

procedure PutFormInScreenCenter(Form: TBaseForm);
procedure SetControlData(Container: TWinControl; Obj: TDzPersistent);
procedure GetControlData(Container: TWinControl; Obj: TDzPersistent);
procedure SetControlState(Container: TWinControl; State: Boolean);

implementation

procedure PutFormInScreenCenter(form: TBaseForm);
var
  wr: TRect;
begin
  wr := Screen.WorkAreaRect;
  if Form.WindowState = wsNormal then
  begin
    Form.Left := wr.Left + (wr.Right - wr.Left - Form.Width) div 2;
    Form.Top := wr.Top + (wr.Bottom - wr.Top - Form.Height) div 2;
  end;
end;

procedure SetControlState(Container: TWinControl; state: Boolean);
var
  i: Integer;
  ctrl: TControl;
begin
  for i := 0 to Container.ControlCount - 1 do
  begin
    ctrl := Container.Controls[i];
    if (ctrl is TCustomEdit) then
      TCustomEdit(ctrl).ReadOnly := not state
    else if (ctrl is TCustomComboBox) then
      TCustomComboBox(ctrl).Enabled := state
    else if (ctrl is TButtonControl) then
      TButtonControl(ctrl).Enabled := state;
  end;    
end;

procedure SetControlData(Container: TWinControl; obj: TDzPersistent);
var
  i: Integer;
  ctrl: TControl;
begin
  for i := 0 to Container.ControlCount - 1 do
  begin
    ctrl := Container.Controls[i];
    if (ctrl is TCustomEdit) then
      TCustomEdit(ctrl).Text := obj.PropAsString[Copy(ctrl.Name, 4, MaxInt)]
    else if ctrl is TCustomComboBox then
    with TCustomComboBox(ctrl) do
    begin
      ItemIndex := Items.IndexOf(obj.PropAsString[Copy(ctrl.Name, 4, MaxInt)]);
      if ((ItemIndex < 0) and (Items.Count > 0)) then ItemIndex := 0;
    end;
  end;    
end;

procedure GetControlData(Container: TWinControl; obj: TDzPersistent);
var
  i: Integer;
  ctrl: TControl;
begin
  for i := 0 to Container.ControlCount - 1 do
  begin
    ctrl := Container.Controls[i];
    if (ctrl is TCustomEdit) then
      obj.PropAsString[Copy(ctrl.Name, 4, MaxInt)] := TCustomEdit(ctrl).Text
    else if (ctrl is TComboBox) then
    with TComboBox(ctrl) do
      obj.PropAsString[Copy(ctrl.Name, 4, MaxInt)] := Text;
  end;    
end;

{ TBaseForm }

function TBaseForm.CenterForm: Boolean;
begin
  Result := True;
end;

function CreatePanel(PanelType: TComponentClass; Form: TForm; Width, Height: Integer;
  BgColor: TColor): TPanel;
begin
  Result := TPanel(PanelType.Create(Form));
  Result.BevelInner := bvNone;
  Result.BevelKind := bkNone;
  Result.BevelOuter := bvNone;
  Result.Caption := '';
  Result.Width := Width;
  Result.Height := Height;
  Result.Color := BgColor;
end;

procedure TBaseForm.CreateParams(var Params: TCreateParams);
begin
  inherited;
  if ShowTaskBarIcon then Params.WndParent := 0;
  Params.Style := Params.Style or WS_THICKFRAME;
end;

function TBaseForm.ShowTaskBarIcon: Boolean;
begin
  Result := False;
end;

procedure TBaseForm.WMSize(var msgr: TWMSize);
begin
  try
    inherited;
    if not CenterForm then Exit;
    PutFormInScreenCenter(Self);
  except
  end;
end;

{ TLBSObjectForm }

destructor TLBSObjectForm.Destroy;
begin
  inherited;
end;

procedure TLBSObjectForm.SetObj(const Value: TLBSObject);
begin
  if fObj = Value then Exit;
  {if fObj <> nil then fObj.Free;
  fObj := TLBSObjectClass(Value.ClassType).Create;
  fObj.Assign(Value);}
  fObj := Value;
  Changed;
end;

procedure TLBSObjectForm.SetObjType(const Value: TLBSObjectType);
begin
  fObjType := Value;
  Changed;
end;

end.
